package com.apes.fn.fico.prepaid.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.DutyCenter;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Title: PrepaidExpense
 * @Description: 待摊费用表
 * @author: ZhengZeXian
 * @create: 2022/2/10 15:49
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PrepaidExpense.CONST_TABLE_NAME)
public class PrepaidExpense extends Model {

    public static final String CONST_TABLE_NAME = "fn_prepaid_expense";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "待摊费用表", padding = 5, prefix = "DT%(company)%(yy)%(month)", useDateRange = true)
    @Comment("待摊费用编码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "duty_center_id", referencedColumnName = "id")
    @Comment("成本中心")
    private DutyCenter dutyCenter;

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company;

    @States(name = "单据状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "approve", name = "审核"),
            @State(id = "share", name = "分摊中"),
            @State(id = "finish", name = "分摊完成"),
            @State(id = "delete", name = "删除")
    })
    @Column(name = "state", length = 10, nullable = false)
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_id", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveId;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Column(name = "start_share_date")
    @Comment("开始分摊日期")
    private Date startShareDate;

    @Comment("备注")
    @Column(name = "remark", length = 250)
    private String remark;

    @Comment("待摊费用明细")
    @OneToMany(mappedBy = "prepaidExpense", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<PrepaidExpenseItem> prepaidExpenseItems;

    public List<PrepaidExpenseItem> getPrepaidExpenseItems() {
        if (Objects.isNull(prepaidExpenseItems)) return new ArrayList<>();
        for (PrepaidExpenseItem item : prepaidExpenseItems) {
            item.setPrepaidExpense(this);
        }
        return prepaidExpenseItems;
    }

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if (this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

}
